package com.ymatou.productstock.api.testcase;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productstock.service.ReduceProductStockForOrderCall;
import com.ymatou.iapi.sellerproduct.service.AddFbxProductCall;
import com.ymatou.productstock.api.service.EngrossFBXStockCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtEngrossStockRecord;
import com.ymttest.database.model.YmtStockChangeDetail;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtEngrossStockRecordWapper;
import com.ymttest.database.sqlwapper.YmtStockChangeDetailWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.JsonBeanFile;

public class Ts_EngrossFBXStock {
	//base data
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	//sql db
	private static YmtCatalogsIWapper ymtCatalogsIWapper=new YmtCatalogsIWapper();
	
	
	//API
	private static EngrossFBXStockCall engrossFBXStockCall=new EngrossFBXStockCall();
	private static ReduceProductStockForOrderCall reduceProductStockForOrderCall=new ReduceProductStockForOrderCall();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("FBX 支付预占用");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(engrossFBXStockCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}

	//FBX 支付预占用
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_EngrossFBXStock_001() {
		Logger.start(true, "FBX 支付预占用");
		try{
			//发布商品
			String skuStr = "YM00013";	
			String productId = new AddFbxProductCall().callAndGetReturnData(MapUtil.hashMap("UserID", sellerId, "LoginID", sellerName,"Product->Catalogs[0]->SKU",skuStr)).getJSONObject("Data").getString("ProductID");
				
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			
			String catalogsId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
		
			int BuyerId = Math.abs(TestDataManager.getRandomIntNum(4)); 
			int OrderId = Math.abs(TestDataManager.getRandomIntNum(8)); 
			int buyNum = -1;
			
			//===================================================================================================================
			//扣减库存
			Map<String, Object> reduceUpdateMap = new HashMap<String, Object>();
			reduceUpdateMap.put("CatalogList[0]->CatalogId", catalogsId);
			reduceUpdateMap.put("CatalogList[0]->CatalogType", 2);// 备货方式（0-现货，1-代购，2-FBX）
			reduceUpdateMap.put("CatalogList[0]->ProductId", productId);
			reduceUpdateMap.put("CatalogList[0]->SellerId", sellerId);
			reduceUpdateMap.put("CatalogList[0]->BuyCount", buyNum);
			reduceUpdateMap.put("CatalogList[0]->BuyerId", BuyerId);
			reduceUpdateMap.put("CatalogList[0]->OrderId", OrderId);
			
			/*JSONObject retBean = reduceProductStockForOrderCall.callAndGetReturnData(reduceUpdateMap);
			Logger.verifyEquals(200, retBean.getInt("Code"), "响应报文-返回码");*/
			
			Map<String, Object> updateMap = MapUtil.hashMap("SellerID", sellerId, "OrderID", OrderId, 
															"LockSkus[0]->ProductId", productId,
															"LockSkus[0]->CatalogId",catalogsId,
															"LockSkus[0]->StockStatus",2,
															"LockSkus[0]->PreStockCount",Math.abs(buyNum));
			
			
			//支付预占用
			JSONObject engrossFBXStockBean = JsonBeanHelper.getJsonBean(JsonBeanFile.engrossfbxstockcalltpl);
			JsonBeanHelper.updateJsonBean(engrossFBXStockBean, updateMap);
			engrossFBXStockCall.setData(engrossFBXStockBean);
			engrossFBXStockCall.callService();
			Logger.verifyEquals(true, engrossFBXStockCall.getReturnData().contains("ok"), "响应报文-返回码");	
			
			Thread.sleep(3000);
			//SQL-Ymt_StockChangeDetail
			YmtStockChangeDetail ymtStockChangeDetail = new YmtStockChangeDetailWapper().selectByCatalogId(catalogsId).get(0);
			Map<String, Object> tgtsqlMap2 = MapUtil.hashMap("sSKU",skuStr, //商品规格Id
																"iStock",0,//扣减库存数量
																"sCatalogId",catalogsId); //规格Id
			
			AssertService.assertResultEqual(tgtsqlMap2, MapUtil.pojoToMap(ymtStockChangeDetail),"SQL-Ymt_StockChangeDetail ");
			
			//SQL-Ymt_EngrossStockRecord
			//20180428 fbx去除改造 JIRACONN-20890
//			YmtEngrossStockRecord ymtEngrossStockRecord = new YmtEngrossStockRecordWapper().selectByOrderId(OrderId);
//			Logger.verifyEquals(Math.abs(buyNum), ymtEngrossStockRecord.getiStock(), "SQL-Ymt_EngrossStockRecord stock");
			
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
